package com.umeox.watch.moto.dataservice.proxy;

import android.os.IBinder;
import android.os.RemoteException;

import com.umeox.watch.moto.apiclient.model.VoipTokenDTO;
import com.umeox.watch.moto.dataservice.BuildConfig;
import com.umeox.watch.moto.dataservice.interfaces.IVideocall;

import net.vidageek.mirror.dsl.Mirror;

public class VideocallProxy implements IVideocall {
    private IBinder mRemote;

    private VideocallProxy(IBinder remote) {
        mRemote = remote;
    }

    public static IVideocall fetchBinder() {
        try {
            return new VideocallProxy((IBinder) new Mirror().on("android.os.ServiceManager").invoke().method("getService").withArgs(BuildConfig.videlcallBinderName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public IBinder asBinder() {
        return mRemote;
    }


    @Override
    public VoipTokenDTO getVoipToken(String hid, String tuid, String channel) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        VoipTokenDTO result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(hid);
            data.writeString(tuid);
            data.writeString(channel);
            mRemote.transact(TRANSACTION_GET_VIDEO_TOKEN, data, reply, 0);
            reply.readException();
            result = reply.readParcelable(VoipTokenDTO.class.getClassLoader());
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public int inviteJoinVideo(String friendId, String channel) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        int result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(friendId);
            data.writeString(channel);
            mRemote.transact(TRANSACTION_INVITE_JOIN_VIDEO, data, reply, 0);
            reply.readException();
            result = reply.readInt();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public int inviteJoinVoip(String friendId, String channel) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        int result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(friendId);
            data.writeString(channel);
            mRemote.transact(TRANSACTION_INVITE_JOIN_VOIP, data, reply, 0);
            reply.readException();
            result = reply.readInt();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public int inviteJoinPTT(String friendId, String channel) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        int result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(friendId);
            data.writeString(channel);
            mRemote.transact(TRANSACTION_INVITE_JOIN_PTT, data, reply, 0);
            reply.readException();
            result = reply.readInt();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public boolean notifyCancelVideo(String friendId, String channel) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        boolean result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(friendId);
            data.writeString(channel);
            mRemote.transact(TRANSACTION_NOTIFY_CANCEL_VIDEO, data, reply, 0);
            reply.readException();
            result = reply.readByte() == 1;
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public boolean notifyCancelVoip(String friendId, String channel) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        boolean result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(friendId);
            data.writeString(channel);
            mRemote.transact(TRANSACTION_NOTIFY_CANCEL_VOIP, data, reply, 0);
            reply.readException();
            result = reply.readByte() == 1;
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public boolean notifyCancelPTT(String friendId, String channel) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        boolean result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(friendId);
            data.writeString(channel);
            mRemote.transact(TRANSACTION_NOTIFY_CANCEL_PTT, data, reply, 0);
            reply.readException();
            result = reply.readByte() == 1;
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public boolean uploadVideoTime(String friendId, String channel, long startTime, long endTime) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        boolean result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(friendId);
            data.writeString(channel);
            data.writeLong(startTime);
            data.writeLong(endTime);
            mRemote.transact(TRANSACTION_UPLOAD_VIDEO_TIME, data, reply, 0);
            reply.readException();
            result = reply.readByte() == 1;
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }
}
